-
Notifications
You must be signed in to change notification settings - Fork 15
/
vpath.cpp
84 lines (73 loc) · 1.8 KB
/
vpath.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// GitHub ID: @Shantanugupta1118
// Day 8 of 100
// Valid Paths - codechef/MAY21
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define itr(i, start, end) for(int i = start; i < end; i++)
#define MOD 1000000007
#define mk make_pair
#define uniq(v) (v).erase(unique(all(v)), (v).end())
#define ff first
#define ss second
#define rf(i, start, end) for(int i = start; i >= end; i--)
#define sc(a) scanf("%lld", &a)
#define psf push_front
#define ppf pop_front
#define ppb pop_back
#define pb push_back
#define pq priority_queue
#define all(s) s.begin(),s.end()
#define sp(a) setprecision(a)
#define rz resize
#define ld long double
#define inf (ll)1e18
#define ub upper_bound
#define lb lower_bound
#define bs binary_search
#define eb emplace_back
#define sz(a) (int)(a.size())
const double pi = acos(-1);
ll binpow(ll a, ll b, ll md) {ll res = 1;while(b!=0) {if(b&1)res *= a, res %=md;a *= a, a%=md; b>>=1;}return res%md;}
ll ans;
vector<vector<int>> v;
vector<ll> dp, tot;
void dfs(int curr, int p) {
dp[curr]=1, tot[curr]=1;
ll sum = 0, count = 0;
itr(i, 0, sz(v[curr])) {
ll node = v[curr][i];
if(node != p) {
dfs(node, curr);
dp[curr] += ((2*dp[node])%MOD), dp[curr]%=MOD, count++, tot[curr]+=tot[node], tot[curr]%=MOD, tot[curr]+= dp[node], tot[curr]%=MOD;
sum += dp[node];
}
}
itr(i, 0, sz(v[curr])) {
int node = v[curr][i];
if(node != p) {
tot[curr] += (dp[node]*((sum - dp[node] + MOD)%MOD))%MOD;
tot[curr] %= MOD;
}
}
}
int main() {
ios_base::sync_with_stdio(false), cin.tie(NULL);
int tc;
cin >> tc;
itr(i, 1, tc+1) {
ans = 0;
int n;
cin >> n;
v.rz(n+1), dp.rz(n+1), tot.rz(n+1);
itr(i, 0, n-1) {
int l, r;
cin>>l>>r;
v[l].pb(r), v[r].pb(l);
}
dfs(1, 1);
ll ans = tot[1]%MOD;
cout<<ans<<endl;
dp.clear(), v.clear(), tot.clear();
}
}